/*
 * Таблица справочника Страны
 * 
 */
package mops.view.window.country;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.util.Callback;
import mops.commons.Commons;
import mops.commons.table_columns.TableImageColumn;
import mops.view.model.Country;

/**
 *
 * @author Жук Александр
 */
public class CountryTable extends TableView<Country> {
    
    private final String COUNTRY_OBJECT_COUNT_TITLE = "Численность объектов";
    private final int COUNTRY_OBJECT_COUNT_WIDTH = 200;
    private final String COUNTRY_OBJECT_COUNT_PROPERTY = "countryCount";
    
    private final String COUNTRY_FLAG_TITLE = "Страна";
    private final int COUNTRY_FLAG_WIDTH = 200;
    private final String COUNTRY_FLAG_PROPERTY = "countryFlag";
       
    public CountryTable(){
        
        this.setTableMenuButtonVisible(true);
        
        TableColumn<Country,String> countrCountColumn = new TableColumn<Country,String>(this.COUNTRY_OBJECT_COUNT_TITLE);
        countrCountColumn.setCellValueFactory(new PropertyValueFactory(this.COUNTRY_OBJECT_COUNT_PROPERTY));
        countrCountColumn.setPrefWidth(this.COUNTRY_OBJECT_COUNT_WIDTH);
        
        //Выравнивание текста по центру
        countrCountColumn.setCellFactory(
                new Callback<TableColumn<Country, String>, TableCell<Country, String>>() {
                    @Override
                    public TableCell<Country, String> call(TableColumn<Country, String> param) {
                        TableCell<Country, String> cell = new TableCell<Country, String>(){
                            @Override
                            public void updateItem(String item, boolean empty) {
                                if (item != null){
                                 setText(item);
                            }
                        }  
                        };
                        cell.setAlignment(Pos.CENTER);
                        return cell;
                }
            });
        
        TableColumn<Country,TableImageColumn> countryFlagColumn = new TableColumn<Country,TableImageColumn>(this.COUNTRY_FLAG_TITLE);
        countryFlagColumn.setCellValueFactory(new PropertyValueFactory(this.COUNTRY_FLAG_PROPERTY));
        countryFlagColumn.setPrefWidth(this.COUNTRY_FLAG_WIDTH);
        
        //Переопределение отрисовки колонки для втсавки изображения
        countryFlagColumn.setCellFactory(
                new Callback<TableColumn<Country,TableImageColumn>,TableCell<Country,TableImageColumn>>(){
                    @Override
                    public TableCell<Country,TableImageColumn> call(TableColumn<Country,TableImageColumn> param){
                        TableCell<Country,TableImageColumn> cell = new TableCell<Country,TableImageColumn>(){
                            @Override
                            public void updateItem(TableImageColumn item, boolean empty){
                                if (item != null){
                                    HBox box = new HBox();
                                    box.setSpacing(10);
                                    VBox vbox = new VBox();
                                    ImageView imageview = new ImageView();
                                    imageview.setFitHeight(item.getImageFile().getHeight());
                                    imageview.setFitWidth(item.getImageFile().getWidth());
                                    imageview.setImage(item.getImageFile());
                                    box.getChildren().addAll(imageview,vbox);
                                    setGraphic(box);
                                    this.setText(item.getCaptionText());
                                    this.setAlignment(Pos.CENTER_LEFT);
                                }
                            }
                        };
                        return cell;
                    }
        });
           
        //Пример данных
        this.getColumns().addAll(countryFlagColumn,countrCountColumn);
        ObservableList<Country> countrys = FXCollections.observableArrayList();
        for (int i=0; i<3; i++){
            TableImageColumn tic = new TableImageColumn("Дания",new Image(this.getClass().getResource(Commons.FLAG_DENMARK_PICTURE).toString()));
            Country m = new Country("12", tic);
            countrys.add(m);
        }
      
        this.setItems(countrys);
    }
    
}
